React + TypeScript: Using Inline Styles Correctly |
您所在的位置:网站首页 › react typescript layout styled › React + TypeScript: Using Inline Styles Correctly |
When using inline styles in a React project that is written in TypeScript, you may encounter some annoying problems. For example, the code below: // App.tsx // Kindacode.com import React from "react"; const App: React.FunctionComponent = () => { return ; }; const styles = { container: { fontWeight: "bold", }, }; export default App;will result in an error like this: /Users/goodman/Desktop/Test/react_ts_kindacode/src/App.tsx TypeScript error in /Users/goodman/Desktop/Test/react_ts_kindacode/src/App.tsx(5,15): Type '{ fontWeight: string; }' is not assignable to type 'Properties'. Types of property 'fontWeight' are incompatible. Type 'string' is not assignable to type 'FontWeight | undefined'. TS2322 3 | import React from "react"; 4 | const App: React.FunctionComponent = () => { > 5 | return ; | ^ 6 | }; 7 | 8 | const styles = {Similar errors can also occur with some common properties like flexDirection, position, etc. Table Of Contents 1 Solutions 1.1 1. Casting styles as const 1.2 2. Using { [key: string]: React.CSSProperties } 1.3 3. Casting each child of styles as React.CSSProperties 2 Conclusion SolutionsWe have more than one solution to get rid of the above issue. 1. Casting styles as const // App.tsx import React from "react"; const App: React.FunctionComponent = () => { return ( Kindacode.com ); }; const styles = { container: { width: 300, height: 300, margin: '50px auto', backgroundColor: "orange", display: "flex", flexDirection: "column", justifyContent: 'center', alignItems: 'center', fontWeight: "bold", }, } as const; export default App;Screenshot: 2. Using { [key: string]: React.CSSProperties }You can modify the code of the first solution as this: const styles: { [key: string]: React.CSSProperties } = { container: { width: 300, height: 300, margin: "50px auto", backgroundColor: "orange", display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", fontWeight: "bold", }, }; 3. Casting each child of styles as React.CSSPropertiesYou can change the code in the first solution to this: const styles = { container: { width: 300, height: 300, margin: "50px auto", backgroundColor: "orange", display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", fontWeight: "bold", } as React.CSSProperties, }; ConclusionWe’ve gone through a few approaches to make TypeScript happy and stop warning when using inline styles. If you would like to explore more about modern React and TypeScript, take a look at the following: React + TypeScript: Create an Autosize Textarea from scratch React + TypeScript: Handling Select onChange Event React + TypeScript: Handling onFocus and onBlur events React & TypeScript: Using useRef hook example React + TypeScript: Drag and Drop Example React + TypeScript: Using setTimeout() with Hooks React + TypeScript: setInterval() example (with hooks)You can also check our React category page and React Native category page for the latest tutorials and examples. Share Tweet Telegram Subscribe 0 Comments Inline Feedbacks View all commentsRelated Articles 3 Best Libraries to Create Pie Charts in React March 6, 2024 Most popular React Component UI Libraries (2024) March 6, 2024 React warning: findDOMNode is deprecated in StrictMode (2 solutions) March 6, 2024 Top 4 best React form validation libraries (2024) March 6, 2024 How to create a Filter/Search List in React (updated) March 6, 2024 5 best open-source WYSIWYG editors for React (2024) March 6, 2024 5 Best Open-Source HTTP Request Libraries for React (2024) January 24, 2024 Best Libraries for Formatting Date and Time in React (2024) January 24, 2024 Best open-source Admin Dashboard libraries for React (2024) January 24, 2024 Best open-source Icon libraries for React (2024) January 24, 2024 React: 5+ Ways to Store Data Locally in Web Browsers (2024) January 24, 2024 React + TypeScript: Handling onClick event September 6, 2023 React + TypeScript: Handling form onSubmit event September 6, 2023 React Router Dom: Parsing Query String Parameters March 3, 2023 React: You are running create-react-app 4.x which is behind the latest release (5.x) March 3, 2023 React: How to Create a New Project in the Current Directory March 3, 2023 Displaying the Current Year in React March 3, 2023 React: How to Disable a Button after One Click March 3, 2023 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |